<?php

MainDesign::no_cache();

add_mini_help('foresee');

require 'Processing/Subdivisions/subdivision_data_processing.php';
require 'DB/db_data_loader.php';
$subdivisions = get_subdivisions_associative();
$dates = SubdivisionData::get_dates_predicted(12);

if (count($dates) < 1 || count($subdivisions) < 1) {
    echo "Nėra pradinių sistemos duomenų pagal kuriuos būtų galima atlikti pakeitimų įtakos analizę. Pirma importuokite juos.";
    return;
} else {
    if (isset($saved_request)) {
        $request = $saved_request;
    } else if ($_POST && array_key_exists('new_measure', $_POST)) {
        $request = $_POST;
    } else {
        $request = array();
    }

    require 'foresee_menu.php';

    MainDesign::leftSubmenuBeginning("Rodyti", 'show_menu');
    MainDesign::viewElementsList();
    MainDesign::leftSubmenuEnding();

    MainDesign::h1("Pakeitimų įtaka");

    /* New elements */
    require 'ForeseeDesign.php';
    MainDesign::viewSectionBegin('Paramos priemonių įvedimas', true);
    ForeseeDesign::show_input($subdivisions, SubdivisionData::get_predicted_dates($dates), $request);
    MainDesign::viewSectionEnd();

    /* Diagrams */
    if ($request) {
        require_once 'foresee_content.php';

        //$hasData comes from foresee_menu
        if ($hasData || !empty($_GET['saved'])) {
            ForeseeDiagrams::draw($request, $subdivisions, $dates);

            //add_error($msg)
            //messages("error")
        }
    }
    messages("error");

    if (($_POST && isset($last_saved_id)) || isset($saved_id)) {
        if (!isset($saved_id) && isset($last_saved_id)) {
            $saved_id = $last_saved_id;
        }
        if (isset($saved_id) && is_int($saved_id)) {
            $tab = Addresing::get_requested_tab();
            MainDesign::buttonPrint($tab, "&amp;saved=$saved_id");
        }
    }
}